package com.joel.crm.model;

import com.joel.crm.customer.Customer;

public class DoubleLinkedlist{


	private Node head;
	private int size;
	private Node tail;
	
	public void delete(Node node) {
		Node tmp = node;
		if (tmp != null) {
			if (tmp.getPreNode() == null) {
				head = tmp.getNextNode();
				head.setPreNode(null);
			} else if (tmp.getNextNode() == null) {
				tail = tail.getPreNode();
				tail.setNextNode(null);
			} else {
				tmp.getPreNode().setNextNode(tmp.getNextNode());
				tmp.getNextNode().setPreNode(tmp.getPreNode());
			}
			size--;
		}
	}
	
	public void add(Customer data) {
		Node node = new Node(data);
		if (head == null) {
			head = node;
			tail = node;
		} else if (head == tail) {
			tail = node;
			head.setNextNode(node); // 将两个节点关联起来
			node.setPreNode(head);
		} else {
			node.preNode = tail;
			tail.nextNode = node;
			tail = node;
		}
		size++;
	}
	
	public int size() {
		return size;
	}
	
	public Node getHead(){
		return head;
	}

	public void show() {
		Node cursorNode = head;
		System.out.println("Customer information: ");
		while (cursorNode != null) {
			System.out.println(cursorNode.getData().getId() + " "
					+ cursorNode.getData().getName() + " "
					+ cursorNode.getData().getSex() + " "
					+ cursorNode.getData().getBirth() + " "
					+ cursorNode.getData().getAddress() + " "
					+ cursorNode.getData().getPhone());
			cursorNode = cursorNode.getNextNode();
		}

	}
}
